/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core.builders
{
	import funit.core.FUnitFramework;
	import funit.core.ITest;
	import funit.core.RunState;
	import funit.core.TestFixture;
	import funit.core.TestSuite;
	
	import mx.utils.StringUtil;
	
	import sv.reflection.MemberFlags;
	import sv.reflection.MethodInfo;
	import sv.reflection.SpecialTypes;
	import sv.reflection.Type;
	import sv.utils.AttributeUtil;
	
	public class TestFixtureBuilder
	{
		
		/**
		 * @param fixture
		 * @return
		 */
		public static function buildFrom( fixture:Object ) : TestSuite
		{
			if (fixture is Class)
			{
				return buildFromDefinition(fixture as Class);
			}
			else if (fixture is Type)
			{
				return buildFromType(fixture as Type);
			}
			else
			{
				return buildFromInstance(fixture);
			}
		}
		
		/**
		 * @param definition
		 * @return
		 */
		public static function buildFromDefinition( definition:Class ) : TestSuite
		{
			return	buildFromType(
						Type.getType(definition)
					);
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		public static function buildFromType( fixtureType:Type ) : TestSuite
		{
			var suite:TestSuite = createTestSuite(fixtureType);
			
			setTestSuiteProperties(fixtureType, suite);
			addTestCases(fixtureType, suite);
			
			if (suite.runState != RunState.NotRunnable && suite.testCount == 0)
			{
				suite.runState = RunState.NotRunnable;
				suite.ignoreReason = StringUtil.substitute( "Suite '{0}' doesn't have any valid tests.", suite.testName.name );
			}
			
			return suite;
		}
		
		/**
		 * @param fixture
		 * @return
		 */
		public static function buildFromInstance( fixture:Object ) : TestSuite
		{
			return	buildFromType(
						Type.getType(fixture)
					);
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		private static function createTestSuite( fixtureType:Type ) : TestSuite
		{
			var suite:TestSuite = new TestFixture(fixtureType);
			
			return suite;
		}
		
		/**
		 * @param fixtureType
		 * @return
		 */
		private static function getTestMethodCandidates( fixtureType:Type ) : Array
		{
			return fixtureType.getMembers(MemberFlags.Method);
		}
		
		/**
		 * @param fixtureType
		 * @param testSuite
		 */
		private static function setTestSuiteProperties( fixtureType:Type, testSuite:TestSuite ) : void
		{
			var ignoreInfo:IgnoreInfo = new IgnoreInfo();
			
			if ( !isValidFixtureType(fixtureType, ignoreInfo) )
			{
				testSuite.runState = RunState.NotRunnable;
				testSuite.ignoreReason = ignoreInfo.reason;
			}
			
			FUnitFramework.applyCommonAttributes( fixtureType.attributes, testSuite );
		}
		
		/**
		 * @param type
		 * @param testSuite
		 */
		private static function addTestCases( type:Type, testSuite:TestSuite ) : void
		{
			var methods:Array = getTestMethodCandidates(type);
			for each ( var method:MethodInfo in methods )
			{
				var test:ITest = TestCaseBuilder.build(method);
				
				if (test != null)
				{
					testSuite.add(test);
				}
			}
		}
		
		/**
		 * @param fixtureType
		 * @param ignoreInfo
		 * @return
		 */
		private static function isValidFixtureType( fixtureType:Type, ignoreInfo:IgnoreInfo ) : Boolean
		{
			if (fixtureType.constructor.parameterCount != 0)
			{
				ignoreInfo.reason = StringUtil.substitute( "Suite '{0}' does not have a valid constructor.", fixtureType.fullName );
				return false;
			}
			
			return ( checkSetUpTearDownMethod(fixtureType, "SetUp", ignoreInfo) &&
					 checkSetUpTearDownMethod(fixtureType, "TearDown", ignoreInfo) &&
					 checkSetUpTearDownMethod(fixtureType, "TestFixtureSetUp", ignoreInfo) &&
					 checkSetUpTearDownMethod(fixtureType, "TestFixtureTearDown", ignoreInfo) );
		}
		
		/**
		 * @param fixtureType
		 * @param alias
		 * @param ignoreInfo
		 * @return
		 */
		private static function checkSetUpTearDownMethod( fixtureType:Type, alias:String, ignoreInfo:IgnoreInfo ) : Boolean
		{
			var members:Array =
				AttributeUtil.getMembersWithAttributeAlias(
					fixtureType,
					alias,
					MemberFlags.Method,
					true
				);
			
			if (members.length == 0)
			{
				return true;
			}
			
			if (members.length > 1)
			{
				ignoreInfo.reason = StringUtil.substitute( "More than one '{0}' method was declared.", alias );
				return false;
			}
			
			var method:MethodInfo = members[0] as MethodInfo;
			
			if ( method.isStatic ||
				 method.parameterCount > 0 ||
				 method.returnType != SpecialTypes.Void )
			{
				ignoreInfo.reason = StringUtil.substitute( "Declared '{0}' method has an invalid signature.", alias );
				return false;
			}
			else
			{
				return true;
			}
		}
		
	}
	
}

internal class IgnoreInfo
{
	
	public var reason:String;
	
}
